package com.hrb.monkey;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aitsuki.swipe.SwipeItemLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.hrb.monkey.record.User;
import com.hrb.monkey.utils.ToastUtil;

import java.util.List;

import javax.sql.DataSource;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.Vholder> {

    private List<User> userList;

    @NonNull
    @Override
    public Vholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_recyclerview, viewGroup, false);
        Vholder vholder = new Vholder(view);
        return vholder;
    }


    @Override
    public void onBindViewHolder(@NonNull final Vholder vholder, int i) {
        User user = userList.get(i);
        vholder.textView.setText(user.getMessage());
        //Glide.with(CustomApplication.getContext()).load("http://192.168.13.78:8080/mvc/wangfei.jpg").override(80,80).centerCrop().into(vholder.imageView);
        Glide.with(CustomApplication.getContext()).load(user.getIconUrl()).listener(requestListener).override(80, 80).centerCrop().into(vholder.imageView);
        vholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.show("viewwwww  " + v.getId());
            }
        });
        if (vholder.rightMenu != null) {
            vholder.rightMenu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtil.show("view  " + userList.get(vholder.getAdapterPosition()).getMessage());
                    //userList.get(vholder.getAdapterPosition()).setMessage("----------");
                    userList.remove(vholder.getAdapterPosition());
                    notifyDataSetChanged();
                    vholder.mSwipeItemLayout.close();
                }
            });
        }

    }

    RequestListener requestListener=new RequestListener() {
        @Override
        public boolean onException(Exception e, Object model, Target target, boolean isFirstResource) {
           // Log.e(TAG, "model:" + model + " isFirstResource-false: " + isFirstResource);
            return false;
        }

        @Override
        public boolean onResourceReady(Object resource, Object model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {
            //Log.d(TAG, "model:" + model + " isFirstResource-ready: " + isFirstResource);
            return false;
        }
    };



    @Override
    public int getItemCount() {
        return userList.size();
    }

    static class Vholder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_image)
        ImageView imageView;
        @BindView(R.id.item_message)
        TextView textView;
        @BindView(R.id.right_menu)
        View rightMenu;
        @BindView(R.id.swipe_layout)
        SwipeItemLayout mSwipeItemLayout;
        /*@BindView(R.id.left_menu)
        private View leftMenu;*/

        public Vholder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public UserAdapter(List<User> userList) {
        this.userList = userList;
    }

}
